package com.molichuxing.services.infrastructure.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.services.business.dto.response.OrderDealerPaymentBizDto;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dto.request.create.OrderDealerDeliveryCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.DeliverySlipModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderDealerStartStatusModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.RefuseModifyFto;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDeliveryDto;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDetailCarInfoDto;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDetailStartDto;
import com.molichuxing.services.property.OrderDealerSendCarStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.OrderDealerDeliveryBean;
import com.molichuxing.services.infrastructure.dao.OrderDealerDeliveryMapper;
import com.molichuxing.services.infrastructure.service.OrderDealerDeliveryService;

/**
 * <p>
 * 经销商订单提车表 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderDealerDeliveryService")
public class OrderDealerDeliveryServiceImpl  implements OrderDealerDeliveryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderDealerDeliveryServiceImpl.class);

    @Autowired
    private OrderDealerDeliveryMapper orderDealerDeliveryMapper;

    @Override
    public List<Integer> getCarIdListByOrderCode(Long orderCode) {

        return orderDealerDeliveryMapper.getCarIdsByOrderCode(orderCode);
    }

    @Override
    public List<OrderDealerDetailCarInfoDto> getDetailCarInfoDto(Long orderCode) {

        List<OrderDealerDeliveryBean> orderDealerDeliveryBeanList = orderDealerDeliveryMapper.getCarInfoByOrderCode(orderCode);
        return Convert.toOrderDealerDetailCarInfoDto(orderDealerDeliveryBeanList);
    }

    @Override
    public List<OrderDealerDetailStartDto> getDetailStartDto(Long orderCode, Integer carStatus) {

        List<OrderDealerDeliveryBean> orderDealerDeliveryBeanList = orderDealerDeliveryMapper.getCatStartByOrderCode(orderCode,carStatus);
        return Convert.toOrderDealerDetailStartDto(orderDealerDeliveryBeanList);
    }

    @Override
    public List<TabDto> getDetailStartCountDto(Long orderCode) {
        List<TabDto> tabList = new ArrayList<>();
        TabDto tabDto = null;
        for(OrderDealerSendCarStatusEnum statusEnum : OrderDealerSendCarStatusEnum.values()) {
            int statusCount = orderDealerDeliveryMapper.getDetailStartDto(statusEnum.getValue(),orderCode);
            tabDto = new TabDto();
            tabDto.setCount(statusCount);
            tabDto.setName(statusEnum.getName());
            tabDto.setType(statusEnum.getValue());
            tabList.add(tabDto);
        }
        return tabList;
    }

    @Override
    public Boolean modifyCarStartStatus(OrderDealerStartStatusModifyDto orderDealerStartStatusModifyDto) {

        return (orderDealerDeliveryMapper.updateCarStart(orderDealerStartStatusModifyDto) == 1) ? true : false;
    }

    @Override
    public Boolean modifyCarRefuseStatus(RefuseModifyFto refuseModifyFto) {

        return (orderDealerDeliveryMapper.updateCarRefuse(refuseModifyFto) == 1) ? true : false;
    }

    @Override
    public OrderDealerDeliveryDto getOrderDealerDeliveryInfo(Long orderCode, Integer carId) {

        OrderDealerDeliveryBean orderDealerDeliveryBeanInfo = orderDealerDeliveryMapper.selectOrderDealerDeliveryInfo(orderCode,carId);
        return Convert.toOrderDealerDeliveryDto(orderDealerDeliveryBeanInfo);
    }

    @Override
    public List<Integer> createOrderDealerDelivery(List<OrderDealerDeliveryCreateDto> orderDealerDeliveryList) {
        List<Integer> idList = new  ArrayList<>();
        for (OrderDealerDeliveryCreateDto orderDealerDeliveryDto : orderDealerDeliveryList) {
            OrderDealerDeliveryBean orderDealerDeliveryBeanBean = new OrderDealerDeliveryBean();
            BeanUtils.copyProperties(orderDealerDeliveryDto, orderDealerDeliveryBeanBean);
            int isOk = orderDealerDeliveryMapper.create(orderDealerDeliveryBeanBean);
            if(isOk != 1){
                return null;
            }
            idList.add(orderDealerDeliveryBeanBean.getId());
        }
        return idList;
    }

    @Override
    public Boolean modifyCarChangeStatus(Long orderCode, Integer carId) {

        return (orderDealerDeliveryMapper.updateIsChangedByCarId(orderCode, carId) == 1) ? true : false;
    }

    @Override
    public Boolean modifyDeliverySlip(DeliverySlipModifyDto deliverySlipModifyDto) {

        return (orderDealerDeliveryMapper.updateDeliverySlip(deliverySlipModifyDto) == 1) ? true : false;
    }

    @Override
    public List<Integer> getSendStatusByOrderCode(Long orderCode) {
        return orderDealerDeliveryMapper.getSendStatusByOrderCode(orderCode);
    }

    @Override
    public Long getOrderCodeBycarId(Integer carId) {
        return orderDealerDeliveryMapper.getOrderCodeBycarId(carId);
    }

    @Override
    public Boolean carIdCheck(Integer carId) {
        OrderDealerDeliveryBean bean= orderDealerDeliveryMapper.carIdCheck(carId);
        return bean == null? true:false;
    }

    @Override
    public List<OrderDealerPaymentBizDto> getOrderDealerPayment(List<Long> orderCodeList, Integer carId) {
        return orderDealerDeliveryMapper.getOrderDealerPayment(orderCodeList,carId);
    }

    @Override
    public Boolean modifyCarLock(Integer carId) {
        return (orderDealerDeliveryMapper.modifyCarLock(carId) == 1) ? true : false;
    }

    @Override
    public Integer getTotalMileageByCarId(Integer carId) {
        return orderDealerDeliveryMapper.getTotalMileageByCarId(carId);
    }

}
